package com.cumt.threeway.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cumt.threeway.R;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.fragment.AbstractFragment;
import com.cumt.threeway.fragment.CommentFragment;
import com.cumt.threeway.fragment.ContentFragment;
import com.cumt.threeway.table.ActivitiesEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Jingjing.hu on 2017/4/17.
 */

public class DetailActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private Adapter mAdapter;
    private Toolbar mToolBar;
    private List<Fragment> mList = new ArrayList<>();
    private String []tabTitles = new String[]{"概要", "简介", "评论"};
    public ActivitiesEntity mActivitiesEntity;
    private ImageView mIv;
    private ImageView mIvLove;
    private CommentFragment mCommentFragment = new CommentFragment();
    private boolean isLove = false;
    List<ActivitiesEntity> mLocalList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_layout);
        initView();
        initData();
    }

    private void initData() {
        mActivitiesEntity = ThreeWayData.getInstance().getActivitiesInfoByName(mActivitiesEntity.getName());
        mLocalList = ThreeWayData.getInstance().getMyLoveActivitiesInfo();
        for(ActivitiesEntity activitiesEntity : mLocalList) {
            if(mActivitiesEntity.getId() == activitiesEntity.getId()) {
                mIvLove.setBackgroundResource(R.drawable.detail_zan);
                isLove = true;
                break;
            } else {
                isLove = false;
                mIvLove.setBackgroundResource(R.drawable.detail_no_zan);
            }
        }
        String url = mActivitiesEntity.getIconUrl();
        if(!TextUtils.isEmpty(url)) {
            Picasso.with(this).load(mActivitiesEntity.getIconUrl()).into(mIv);
        }
        mToolBar.setTitle(mActivitiesEntity.getName());
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void initView() {
        mActivitiesEntity = (ActivitiesEntity) getIntent().getSerializableExtra("single");
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        mToolBar.setNavigationIcon(R.mipmap.btn_back_white_press);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mIv = (ImageView) findViewById(R.id.iv_icon);
        mIvLove = (ImageView) findViewById(R.id.iv_love);
        mIvLove.setOnClickListener(this);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mList.add(new AbstractFragment());
        mList.add(new ContentFragment());
        mList.add(mCommentFragment);
        mAdapter = new Adapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        findViewById(R.id.fab).setOnClickListener(this);
        mTabLayout.addOnTabSelectedListener(new MyTabSelectedListener());
        initPoint();
    }

    public ActivitiesEntity getActivitiesEntity() {
        return mActivitiesEntity;
    }

    public void setActivitiesEntity(ActivitiesEntity mActivitiesEntity) {
        this.mActivitiesEntity = mActivitiesEntity;
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        if(v.getId() == R.id.fab) {
            goSubmitInfoActivity();
        } else if(v.getId() == R.id.iv_love) {
            if(!isLove) {
                isLove = true;
                ThreeWayData.getInstance().updateMyLoveActivitiesInfo(true, mActivitiesEntity.getId());
                mIvLove.setBackgroundResource(R.drawable.detail_zan);
                Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
            } else {
                isLove = false;
                ThreeWayData.getInstance().updateMyLoveActivitiesInfo(false, mActivitiesEntity.getId());
                mIvLove.setBackgroundResource(R.drawable.detail_no_zan);
                Toast.makeText(this, "取消收藏", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void goSubmitInfoActivity() {
        Intent intent = new Intent(DetailActivity.this, SubmitInfoActivity.class);
        intent.putExtra("single", mActivitiesEntity);
        startActivity(intent);
    }

    public void initPoint() {
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            View itemView = getTabView(i);
            tab.setCustomView(itemView);
        }
    }

    public View getTabView(int position){
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView tabText = (TextView) view.findViewById(R.id.tab_name);
        tabText.setText(tabTitles[position]);
        if(position == 0) {
            tabText.setTextColor(getResources().getColor(R.color.white));
        }
        return view;
    }

    class MyTabSelectedListener implements TabLayout.OnTabSelectedListener{

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            View itemView = tab.getCustomView();
            TextView tv_tabName = (TextView) itemView.findViewById(R.id.tab_name);
            tv_tabName.setTextColor(getResources().getColor(R.color.white));
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            View itemView = tab.getCustomView();
            TextView tv_tabName = (TextView) itemView.findViewById(R.id.tab_name);
            tv_tabName.setTextColor(getResources().getColor(R.color.black));
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }


    class Adapter extends FragmentStatePagerAdapter {

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ThreeWayConstant.INTENT_KEY_TO_LOGIN_2) {
            if(resultCode == RESULT_OK) {
//                mCommentFragment.getDataFromInfo();
                mCommentFragment.getLocalData();
            } else if(resultCode == -2) {

            }
        } else if(requestCode == ThreeWayConstant.INTENT_KEY_TO_LOGIN_1) {

        }
    }

}
